home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / SysIO / _main.c < prev    next >
C/C++ Source or Header  |  1990-04-07  |  3KB  |  93 lines

  1. /*
  2.  * Libraries and headers for PDC release 3.3 (C) 1989 Lionel Hummel.
  3.  * PDC Software Distribution (C) 1989 Lionel Hummel and Paul Petersen.
  4.  * PDC I/O Library (C) 1987 by J.A. Lydiatt.
  5.  *
  6.  * This code is freely redistributable upon the conditions that this 
  7.  * notice remains intact and that modified versions of this file not
  8.  * be included as part of the PDC Software Distribution without the
  9.  * express consent of the copyright holders.  No warrantee of any
  10.  * kind is provided with this code.  For further information, contact:
  11.  *
  12.  *  PDC Software Distribution    Internet:                     BIX:
  13.  *  P.O. Box 4006             or hummel@cs.uiuc.edu            lhummel
  14.  *  Urbana, IL  61801-8801       petersen@uicsrd.csrd.uiuc.edu
  15.  */
  16.  
  17. /* main.c */
  18.  
  19. /* This startup code prepares the PDC runtime environment.  This involves
  20.  * setting up a UNIX-style device table. It should be preceded by a system 
  21.  * startup such as acrt0 that will parse the command line and set up DOS I/O.
  22.  * If you do not use the PDC I/O libraries, this function is not necessary and
  23.  * may be stubbed to simply call your main().
  24.  */
  25.  
  26. /* A typical order of execution when executing a program is:
  27.  *
  28.  *    Entry    (Typically, that would be acrt0 as mentioned above)
  29.  *    PDC Startup (This function; internally known as __main())
  30.  *    Your program (Beginning with your main(), internally known as _main())
  31.  *    PDC Cleanup (The C-language exit() function, also called by __main()
  32.  *            in case your _main() decides to rts.
  33.  *    Exit    (Jumps back to acrt0 which competes termination)
  34.  *
  35.  */
  36.  
  37. #include <stdio.h>
  38. #include <fcntl.h>
  39. #include <errno.h>
  40. #undef NULL
  41. #include <exec/types.h>
  42. #include <exec/alerts.h>
  43. #include <exec/memory.h>
  44. #include <libraries/dosextens.h>
  45. #include <workbench/startup.h>
  46.  
  47.  
  48. extern void (*_fcloseall)(), (*_freeall)();
  49. extern short _numdev;
  50. extern struct _device *_devtab;
  51. extern void *malloc();
  52.  
  53. long _main(argc, argv)
  54. int   argc;
  55. char *argv[];
  56. {
  57.     /* _fh_stdin and _fh_stdout are set during startup (acrt0) */
  58.     extern long  _fh_stdin,  /* These are DOS filehandles */
  59.                  _fh_stdout,
  60.                  Open();
  61.  
  62. /* Size and allocate the device table.
  63.  */
  64.     _numdev = OPEN_MAX;
  65.     if ((_devtab = malloc(OPEN_MAX*sizeof(struct _device))) == NULL)  {
  66.         Alert(AG_NoMemory, 0L);
  67.         _exit(ENOMEM);
  68.     }
  69.  
  70. /* O_STDIO inhibits close() from Close()'ing the DOS file handles we
  71.  * inherited.
  72.  */
  73.     _devtab[0]._mode = O_RDONLY | O_STDIO;
  74.     _devtab[0]._fileHandle = _fh_stdin;
  75.     stdin = fdopen(0, "r");                 /* stdin */
  76.     _devtab[1]._mode = O_WRONLY | O_STDIO;
  77.     _devtab[1]._fileHandle = _fh_stdout;
  78.     stdout = fdopen(1, "w");                /* stdout */
  79.  
  80.     if ( _devtab[1]._fileHandle ) {
  81.         _devtab[2]._fileHandle = Open("*", MODE_OLDFILE);
  82.         _devtab[2]._mode |= O_WRONLY;    
  83.         stderr = fdopen(2, "w");            /* stderr */
  84.     }
  85.  
  86. #ifdef INT_MAIN
  87.     exit(main(argc, argv));
  88. #else
  89.     main(argc, argv);
  90.     exit(0);
  91. #endif
  92. }
  93.